package model.stats;

import java.util.Iterator;
import java.util.LinkedList;

public class StatMods {

	private int persistentMod;
	private LinkedList<TempMod> tempMod;
	private LinkedList<TempMod> toRemove;
	
	public StatMods() {
		persistentMod = 0;
		tempMod = new LinkedList<TempMod>();
	}

	public int modify(int toMod) {
		Iterator<TempMod> iter = tempMod.iterator();
		while(iter.hasNext()){
			toMod = iter.next().modify(toMod);
		}
		return toMod+persistentMod;
	}
	public void update() {
		Iterator<TempMod> iter = tempMod.iterator();
		while(iter.hasNext()){
			iter.next().update(this);
		}
		while(!toRemove.isEmpty()){
			TempMod removeable = toRemove.pop();
			tempMod.remove(removeable);
		}
	}
	public void addPersistMod(int mod) {
		persistentMod += mod;
	}
	public void addTempMod(int mag, int dur){
		tempMod.add(new TempMod(mag,dur));
	}
	private void remove(TempMod tempMod) {
		toRemove.add(tempMod);
	}
	
	
	private class TempMod {

		private int magnitude;
		private int durationLeft;
		
		public TempMod(int magnitude, int durationLeft) {
			this.magnitude = magnitude;
			this.durationLeft = durationLeft;
		}
		
		public int modify(int modifier) {
			return modifier + magnitude;
		}
		public void update(StatMods mod) {
			durationLeft--;
			if(durationLeft <= 0)
				mod.remove(this);
		}

	}
}
